﻿using DevComponents.DotNetBar;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Framework;
using PlanningGIS.Util;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.SelectQuery
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CtlDrawGeo : ButtonX
    {
        /// <summary>
        /// 
        /// </summary>
        public CtlDrawGeo()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
            this.ColorTable = eButtonColor.Flat;
            this.BackColor = Color.Transparent;
            this.Style = eDotNetBarStyle.StyleManagerControlled;
            AddButton();
        }

        /// <summary>
        /// 绘制的空间对象
        /// </summary>
        public IGeometry Geometry { get; set; }

        /// <summary>
        /// 绘制的对象
        /// </summary>
        public IElement DrawElement { get; set; }

        /// <summary>
        /// 
        /// </summary>
        protected string _Name = "_ToolArea";

        /// <summary>
        /// 地图管理对象，MapControl.Object
        /// </summary>
        public object Hook { get; set; }

        /// <summary>
        /// 获取或设置当前选择的绘制工具对象
        /// </summary>
        public ESRI.ArcGIS.SystemUI.ICommand CurrentCommand { get; set; }

        /// <summary>
        /// 添加绘制工具按钮
        /// </summary>
        private void AddButton(string nameAlias = "")
        {
            this.Tooltip = "绘制图形";
            string dllname = "PlanningGIS.Engine.Controls";
            List<ValueName> btns = new List<ValueName>()
            {
                new ValueName(){Value="ToolDrawRectangle",Name="矩形"+nameAlias},
                new ValueName(){Value="ToolDrawPolygon",Name="多边形"+nameAlias},
                new ValueName(){Value="ToolDrawCircle",Name="圆"+nameAlias},
                new ValueName(){Value="ToolDrawLine",Name="线"+nameAlias},
            };
            foreach (ValueName v in btns)
            {
                ButtonItem btn = new ButtonItem(v.Value, v.Name);
                btn.Tag = Activator.CreateInstance(Type.GetType(dllname + "." + v.Value + "," + dllname));
                string imagename = v.Value.Replace("ToolDraw", "New");
                Bitmap bmp = (Bitmap)(Properties.Resources.ResourceManager.GetObject(imagename));
                if (bmp != null)
                {
                    bmp.MakeTransparent();
                    btn.Image = bmp;
                }                
                btn.Click += new EventHandler(btn_Click);
                if (btn.Tag is IDrawGeometry)
                {
                    IDrawGeometry dg = (btn.Tag as IDrawGeometry);
                    dg.ClearElements += Clear;
                    dg.DrawFinished += ToolCreator_DrawFinished;
                }
                this.SubItems.Add(btn);
            }
            this.Image = (Bitmap)(Properties.Resources.ResourceManager.GetObject(this.GetType().Name));
            this.Tag = ((ButtonItem)this.SubItems[0]).Tag;
            this.Click += new EventHandler(CtlDrawElements_Click);
        }

        /// <summary>
        /// 清除
        /// </summary>
        public void Clear()
        {
            if (DrawElement == null || Hook == null) return;
            IActiveView view = ((Hook as IApplication).Document as IMxDocument).ActiveView;
            IGraphicsContainer grp = view.GraphicsContainer;
            grp.Reset();
            IElement ele = null;
            while ((ele = grp.Next()) != null)
            {
                IElementProperties ep = ele as IElementProperties;
                if (string.IsNullOrWhiteSpace(ep.Name)) continue;
                if (ep.Name.StartsWith(_Name)) grp.DeleteElement(ele);
            }
            view.PartialRefresh(esriViewDrawPhase.esriViewGraphics | esriViewDrawPhase.esriViewGraphicSelection, null, view.Extent);
            DrawElement = null;
        }

        /// <summary>
        /// 绘制完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void ToolCreator_DrawFinished(object sender, DrawArgs e)
        {
            
        }
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void CtlDrawElements_Click(object sender, EventArgs e)
        {
            int idx = this.SubItems.IndexOf("ToolDrawRectangle");
            if (idx != -1)
            {
                CurrentCommand = this.SubItems[idx].Tag as ESRI.ArcGIS.SystemUI.ICommand;
                CurrentCommand.OnCreate((Hook as IFramework).Object);
                CurrentCommand.OnCreate((Hook as IFramework).Object);
                (Hook as IFramework).Tool = CurrentCommand as ITool;
            }                
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            if (btn.Tag == null) return;
            CurrentCommand = btn.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            CurrentCommand.OnCreate((Hook as IFramework).Object);
            (Hook as IFramework).Tool = CurrentCommand as ITool;
        }
    }
}


